import os  # 建立本地目录使用
import time
import requests
import re
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pyautogui
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait

pic_location = 'qTcms_pic'

html_url = 'https://www.mh160.xyz/kanmanhua/2025/393754.html?p=1'
# region 通过请求的方式获得网页，失败
# html_url_header = 'https://www.mh160.xyz/kanmanhua/2025/'
# html_url_mid = '788'+ str(59) + '.html'
# html_url_param = '?p=' + str(1)
# html_url = html_url_header + html_url_mid + html_url_param
# html_response = requests.get(html_url).text
# print(html_url)
# print(html_response)
# endregion,,s，

driver = webdriver.Chrome()
driver.maximize_window()
driver.get(html_url)

# print(len(ele_select))
# 循环获取网页
while True:
    ele_select = driver.find_elements_by_xpath('//select/option')
    print('本章页数：%s', len(ele_select)-1)
    for i in range(1, len(ele_select)):
        print('网页url:%s' % driver.current_url)
        # # url
        # region url相关
        # pic_url_list = ['https://res.gezhengzhongyi.cn:8443/mh160tuku/s/杀戮都市_2025/第256话_78859/0001.jpg']
        # <img src="https://res.gezhengzhongyi.cn:8443/mh160tuku/s/杀戮都市_2025/第256话_78859/0001.jpg" id="qTcms_pic"
        # endregion
        # print(driver.page_source)
        try:
            pic_url_list = re.findall('<img src="(.*?)" id="qTcms_pic', driver.page_source)
        # print(pic_url_list)
            pic_url = pic_url_list[0]  # https://res.gezhengzhongyi.cn:8443/mh160tuku/s/杀戮都市_2025/第256话_78859/0001.jpg
        except IndexError:
            continue
        print('图片url：%s' % pic_url)

        # 文件路径
        pic_file_path_mid = re.findall('第(.*?)话', pic_url)[0]
        pic_file_path_name = pic_url.split('/')[-1]
        pic_file_path = os.getcwd() + os.sep + 'comic' + os.sep + str(pic_file_path_mid) + os.sep + pic_file_path_name
        print('图片存储路径:%s' % pic_file_path)

        # 判断文件路径是否存在，不存在则创建
        if not os.path.exists(os.getcwd()+os.sep+'comic'+os.sep+str(pic_file_path_mid)):
            os.mkdir(os.getcwd()+os.sep+'comic'+os.sep+str(pic_file_path_mid))

        # 图片保存
        try:
            pic_response = requests.get(pic_url)  # 获取网页
            print('请求图片状态码: %s' % pic_response.status_code)
            with open(pic_file_path, 'wb') as f:  # 打开写入到path路径里-二进制文件，返回的句柄名为f
                f.write(pic_response.content)  # 往f里写入r对象的二进制文件
            f.close()
            pic_response.close()
            print('图片下载成功：%s' % pic_url)
        except requests.exceptions.RequestException as e:
            print('图片下载失败,重试:%s, %s' % (pic_url, e))

        # 点击下一页
        driver.find_element_by_id('k_next').click()
        time.sleep(0.1)
        # k_next = WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located((By.ID, 'k_next')))
        # k_next.click()
        print('******************分割符*******************************')
    # 点击下一章
    driver.find_element_by_id('k_Pic_nextArr').click()
    # k_pic_nextArr = WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located((By.ID, 'k_Pic_nextArr')))
    # k_pic_nextArr.click()





# while True:
#
#     print('网页url:%s' % driver.current_url)
#     # # url
#     # region url相关
#     # pic_url_list = ['https://res.gezhengzhongyi.cn:8443/mh160tuku/s/杀戮都市_2025/第256话_78859/0001.jpg']
#     # <img src="https://res.gezhengzhongyi.cn:8443/mh160tuku/s/杀戮都市_2025/第256话_78859/0001.jpg" id="qTcms_pic"
#     # endregion
#     pic_url_list = re.findall('<img src="(.*?)" id="qTcms_pic', driver.page_source)
#     pic_url = pic_url_list[0]  # https://res.gezhengzhongyi.cn:8443/mh160tuku/s/杀戮都市_2025/第256话_78859/0001.jpg
#     print('图片url：%s' % pic_url)
#
#     # 文件路径
#     pic_file_path_mid = re.findall('第(.*?)话', pic_url)[0]
#     pic_file_path_name = pic_url.split('/')[-1]
#     pic_file_path = os.getcwd() + os.sep + 'comic' + os.sep + str(pic_file_path_mid) + os.sep + pic_file_path_name
#     print('图片存储路径:%s' % pic_file_path)
#
#     # 判断文件路径是否存在，不存在则创建
#     if not os.path.exists(os.getcwd()+os.sep+'comic'+os.sep+str(pic_file_path_mid)):
#         os.mkdir(os.getcwd()+os.sep+'comic'+os.sep+str(pic_file_path_mid))
#
#     # 图片保存
#     try:
#         pic_response = requests.get(pic_url)  # 获取网页
#         print('请求图片状态码: %s' % pic_response.status_code)
#         with open(pic_file_path, 'wb') as f:  # 打开写入到path路径里-二进制文件，返回的句柄名为f
#             f.write(pic_response.content)  # 往f里写入r对象的二进制文件
#         f.close()
#         pic_response.close()
#         print('图片下载成功：%s' % pic_url)
#     except requests.exceptions.RequestException as e:
#         print('图片下载失败,重试:%s, %s' % (pic_url, e))
#
#     # 点击下一页
#     if EC.alert_is_present()(driver):
#         driver.find_element_by_id('k_Pic_nextArr')
#     else:
#         driver.find_element_by_id('k_next').click()
#
# print('******************分割符*******************************')





